#!/bin/bash
SOURCE=$0
while [ -h $SOURCE ];do
    DIR=$(cd `dirname $SOURCE`;pwd)
    SOURCE=`readlink $SOURCE`
    [[$SOURCE != /* ]] && SOURCE=$DIR"/"$SOURCE
done
basePath=$(cd `dirname $SOURCE`;pwd)
#echo "basePath="$basePath
clientConf=$basePath"/client.conf"
#echo "clientConf="$clientConf
modelConf=$basePath/model.conf
modelIp=""
modelFile=""

#cat $modelConf|while read line
#do
#    tmpStr=$line
#    echo $tmpStr
#    echo ${tmpStr%:*}
#    if [ ${tmpStr%:*} == "ip" ];then
#        echo ${tmpStr#*:}
#        modelIp=${tmpStr#*:}
#    elif [ ${tmpStr%:*} == "file" ];then
#        modelFile=${tmpStr#*:}
#    fi
#done

function echoUsage(){
echo -e "usage:\n"
echo -e "\tcheck\t--check some file exists\n"
echo -e "\tupload\t--upload file to dest\n"
echo -e "\tdiff\t--diff sourcefile and distfile\n"
echo -e "\tcmd\t--run 'cmd'\n"
echo -e "\ttrust\t--add ssh trust\n"
}


#get model.conf
for line in `cat $modelConf`
do
    tmpStr=$line
    #echo $tmpStr
    #echo ${tmpStr%:*}
    if [ ${tmpStr%:*} == "ip"  ];then
        #echo ${tmpStr#*:}
        modelIp=${tmpStr#*:}
    elif [ ${tmpStr%:*} == "file"  ];then 
        modelFile=${tmpStr#*:}
    fi
done
#echo "modelIp="$modelIp
#echo "modelFile="$modelFile

function addTrust(){
    ./generateDSA.sh
    for line in `cat $clientConf`
    do
        tmpStr=$line
        echo -e "---------------$tmpStr"
        if [ ${tmpStr%:*} == user ];then
            user=${tmpStr#*:}
            echo -e "user=\t$user"
        elif [ ${tmpStr%:*} == passwd ];then
            passwd=${tmpStr#*:}
            echo -e "passwd=\t$passwd"
        elif [ ${tmpStr%:*} == ip ];then
            sshHost="$user@"${tmpStr#*:}
            desthome="~"
            sourcefile=$(cd ~/.ssh/;pwd)/id_rsa.pub
            destfile="$desthome/.ssh/id_rsa.pub.model"
            authfile="$desthome/.ssh/authorized_keys"
            echo -e "====================================================================="
            echo -e "host=\t\t$sshHost"
            echo -e "sourcefile=\t$sourcefile"
            echo -e "desthome=\t$desthome"
            echo -e "destfile=\t$destfile"
            echo -e "authfile=\t$authfile"
            #./addRSATrust.exp $sourcefile $sshHost $destfile $authfile $passwd > /dev/null
            ./addRSATrust.exp $sourcefile $sshHost $destfile $authfile $passwd
            if [ $? = 0 ];then
                echo -e "Add SSH Trust Success !!!!!!!!!!!!!!!!!!!!!!"
            else 
                echo -e "Add SSH Trust Failed XXXXXXXXXXXXXXXXXXXXXXX"
            fi
        fi
    done
}

if [ $# -eq 1 ];then
    if [ $1 = "check" ];then
        for line in `cat $clientConf`
        do
            tmpStr=$line
            #echo -e "tmp=$tmpStr"
            if [ ${tmpStr%:*} == "destfile" ];then
                destfile=${tmpStr#*:}
                continue
            elif [ ${tmpStr%:*} == "ip" ];then
                destip=${tmpStr#*:}
                echo -e "========================================================="
                #echo -e "check file:$destfile"
                sshHost="engineer@"$destip
                result=$(./check.sh $sshHost $destfile)
                #echo -e "result=\t$result"
                echo -e "Operation=\tcheck"
                if [ $result -eq 0 ];then
                    echo -e "Host:\t\t$sshHost"
                    echo -e "File:\t\t$destfile"
                    echo -e "Result:\t\tNot Exists XXXXXXXXXXXXXXXXXXXXXXX"
                else
                    echo -e "Host:\t\t$sshHost"
                    echo -e "File:\t\t$destfile"
                    echo -e "Result:\t\tExists !!!!!!!!!!!!!!!!!!!!!!"
                fi
            fi
        done
    elif [ $1 = "upload" ];then
        for line in `cat $clientConf`
        do
            tmpStr=$line
            if [ ${tmpStr%:*} == "destfile" ];then
                destfile=${tmpStr#*:}
                continue
            elif [ ${tmpStr%:*} == "ip" ];then
                destip=${tmpStr#*:}
                echo -e "========================================================="
                #echo -e "check file:$destfile"
                sshHost="engineer@"$destip
                result=$(./scp.sh $modelFile $sshHost $destfile)
                #echo -e "result=\t$result"
                echo -e "Operation=\tupload"
                if [ $? -eq 0 ];then
                    echo -e "Host:\t\t$sshHost"
                    echo -e "SourceFile:\t$modelFile"
                    echo -e "DestFile:\t$destfile"
                    echo -e "Result:\t\tUpload Success !!!!!!!!!!!!!!!!!!!!!!!!!"
                else
                    echo -e "Host:\t\t$sshHost"
                    echo -e "SourceFile:\t$modelFile"
                    echo -e "DestFile:\t$destfile"
                    echo -e "Result:\t\tUpload Failed!!!!!!!!!!!!!!!!!!!!!!"
                fi
            fi
        done
    elif [ $1 = "diff" ];then
        for line in `cat $clientConf`
        do
            tmpStr=$line
            if [ ${tmpStr%:*} == "destfile" ];then
                destfile=${tmpStr#*:}
                continue
            elif [ ${tmpStr%:*} == "ip" ];then
                destip=${tmpStr#*:}
                echo -e "========================================================="
                #echo -e "check file:$destfile"
                sshHost="engineer@"$destip
                result=$(./diff.sh $sshHost $destfile $modelFile)
                #echo -e "result=\t$result"
                echo -e "Operation=\tdiff"
                if [ $? -eq 0 ]&&[ "$result" != "" ];then
                    echo -e "Host:\t\t$sshHost"
                    echo -e "SourceFile:\t$destfile"
                    echo -e "DestFile:\t$modelFile"
                    echo -e "Result:\t\t$result"
                else
                    echo -e "Host:\t\t$sshHost"
                    echo -e "SourceFile:\t$destfile"
                    echo -e "DestFile:\t$modelFile"
                    echo -e "Result:\t\tFiles are the same!!!!!!!!!!!!!!"
                fi
            fi
        done
    elif [ $1 = "trust" ];then
        addTrust
    elif [ $1 = "cmd" ];then
        for line in `cat $clientConf`
        do
            tmpStr=$line
            if [ ${tmpStr%:*} == "cmd" ];then
                cmd=${tmpStr#*:}
                continue
            elif [ ${tmpStr%:*} == "ip" ];then
                destip=${tmpStr#*:}
                echo -e "========================================================="
                #echo -e "check file:$destfile"
                sshHost="engineer@"$destip
                #result=$(./ssh.exp $sshHost $cmd)
                result=$(ssh $sshHost $cmd)
                #echo -e "result=\t$result"
                echo -e "Operation=\tcmd"
                if [ $? -eq 0 ];then
                    echo -e "Host:\t\t$sshHost"
                    echo -e "CMD:\t\t$cmd"
                    echo -e "Result:\t\t$result"
                else
                    echo -e "Host:\t\t$sshHost"
                    echo -e "CMD:\t\t$cmd"
                    echo -e "Result:\t\tRun CMD Failed!!!!!!!!!!!!!!!!!!!!!!"
                fi
            fi
        done
    else
        echoUsage
    fi
else
    echoUsage
fi

